package com.studyadmin.service.impl;

import com.alibaba.fastjson.JSON;
import com.studyadmin.service.CommonService;
import com.studyadmin.service.ThreadService;
import com.studyadmin.util.JsonUtil;
import com.studyadmin.util.RedisConstant;
import com.studyadmin.util.StringUtilsValue;
import com.studyadmin.dto.RegParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Component
public class CommonServiceImpl implements CommonService {

    @Autowired(required = false)
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private ThreadService threadService;

    @Override
    public JsonUtil getRequestPermissionCode(String emailJson) {
        JsonUtil jsonUtil = new JsonUtil();
        // 非空校验
        if (StringUtils.isBlank(emailJson)){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("参数不合法");
            return jsonUtil;
        }
        // JSON转换，提取email的值
        String email = JSON.parseObject(emailJson).getString("stuEmail").trim();
        // 邮箱校验
        System.out.println("要发送的邮箱"+email);
        if (!StringUtilsValue.checkEmail(email)) {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("邮箱格式不正确");
            return jsonUtil;
        }
        // 随机生成权限码
        String permissionCode = UUID.randomUUID().toString();

        // 存入redis，缓存10s
        redisTemplate.opsForValue().set(RedisConstant.EMAIL_REQUEST_VERIFY + email, permissionCode, RedisConstant.EXPIRE_ONE_MINUTE, TimeUnit.SECONDS);
        jsonUtil.setCode(0);
        jsonUtil.setMsg("成功");
//        jsonUtil.setMsg(permissionCode);
        jsonUtil.setData(permissionCode);
        return jsonUtil;
    }

    @Override
    public JsonUtil sendEmailCode(RegParam loginParam) {
        JsonUtil jsonUtil = new JsonUtil();
        System.out.println("数据："+loginParam);
        if (loginParam == null){
            jsonUtil.setCode(1);
            jsonUtil.setMsg("参数不合法");
            return jsonUtil;
        }

        // 获取权限码和邮箱
        String email = loginParam.getStuEmail();
        String permissionCode = loginParam.getCode();
        // 参数校验
        if (StringUtils.isAnyBlank(email, permissionCode)) {
            jsonUtil.setCode(1);
            jsonUtil.setMsg("参数不合法");
            return jsonUtil;
        }else if (!StringUtilsValue.checkEmail(email)) {
            // 邮箱校验
            jsonUtil.setCode(1);
            jsonUtil.setMsg("邮箱格式不正确");
            return jsonUtil;
        }else {
            // 权限码比对
            String rightCode = redisTemplate.opsForValue().get(RedisConstant.EMAIL_REQUEST_VERIFY + email);
            if (!permissionCode.equals(rightCode)) {
                // 不通过
                jsonUtil.setCode(1);
                jsonUtil.setMsg("非法操作");
                return jsonUtil;
            }
        }

        // 全部通过

        // 随机生成6位数字验证码
        String code = StringUtilsValue.randomSixCode();

        // 正文内容
        String content = "亲爱的执剑人：\n" +
                "您此次的验证码为：\n\n" +
                code + "\n\n" +
                "此验证码5分钟内有效，请立即进行下一步操作。这是计划的一部分。" +
                " 如非你本人操作，请忽略此邮件。\n" +
                "感谢您的使用！";

        // 发送验证码
        threadService.sendSimpleMail(email, "您此次的验证码为：" + code, content);
        // 丢入缓存，设置5分钟过期
        redisTemplate.opsForValue().set(RedisConstant.EMAIL + email, code, RedisConstant.EXPIRE_FIVE_MINUTE, TimeUnit.SECONDS);
        jsonUtil.setCode(0);
        jsonUtil.setMsg("成功");
        return jsonUtil;

    }
}
